<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.archive');

class lrControllerLr_dictionaries extends lrController {

	function __construct()
	{
		parent::__construct();
	}

	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_lr' );
	}


	function Restore( )
	{
		
		global $mainframe;
		$_rows = 0;
		
		$roles_query = "DROP TABLE `#__lr_roles`;
						CREATE TABLE `#__lr_roles` (
  							`id` int(11) NOT NULL,
  							`role_name` varchar(16) NOT NULL,
  							PRIMARY KEY  (`id`)
						) ENGINE=MyISAM DEFAULT CHARSET=utf8;
						INSERT INTO `#__lr_roles` (`id`, `role_name`) VALUES
							(1, 'Player'),
							(2, 'Coach'),
							(3, 'Staff');
						";
		$pointslogic_query = "DROP TABLE `#__lr_pointslogic`;
								CREATE TABLE IF NOT EXISTS `#__lr_pointslogic` (
									  `id` int(11) NOT NULL,
									  `pointslogictype` varchar(10) NOT NULL,
									  `pts4win` tinyint(4) NOT NULL,
									  `pts4draw` tinyint(4) NOT NULL,
									  `pts4lost` tinyint(4) NOT NULL,
									  `pos1strule` varchar(16) NOT NULL,
									  `pos2ndrule` varchar(16) NOT NULL,
									  `pos3rdrule` varchar(16) NOT NULL,
									  PRIMARY KEY  (`id`)
									) ENGINE=MyISAM DEFAULT CHARSET=utf8;
								INSERT INTO `#__lr_pointslogic` (`id`, `pointslogictype`, `pts4win`, `pts4draw`, `pts4lost`, `pos1strule`, `pos2ndrule`, `pos3rdrule` ) VALUES
									(1, 'FIBA', '2', '0', '1', 'points', 'directscore', 'allscore'),
									(2, 'UEFA', '3', '1', '0', 'points', 'allscore', 'directscore'),
									(3, 'NBA', '3', '1', '0', 'wins', 'allscoreavrg', 'directscore'),
									(4, 'EHS', '2', '1', '0', 'points', 'directscore', 'allscore');";
									
		$stats_query = "DROP TABLE `#__lr_stats_measures`;
						DROP TABLE `#__lr_stats_sets`;
						DROP TABLE `#__lr_stats_setelements`;
						
						CREATE TABLE IF NOT EXISTS `#__lr_stats_measures` (
						  `id` int(11) NOT NULL auto_increment,
						  `name` varchar(64) character set utf8 collate utf8_polish_ci NOT NULL,
						  `shortname` varchar(4) character set utf8 collate utf8_polish_ci NOT NULL,
						  `format` varchar(16) NOT NULL,
						  `description` varchar(256) character set utf8 collate utf8_polish_ci NOT NULL,
						  PRIMARY KEY  (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
						
						CREATE TABLE IF NOT EXISTS `#__lr_stats_sets` (
						  `id` int(11) NOT NULL auto_increment,
						  `name` varchar(64) character set utf8 collate utf8_polish_ci NOT NULL,
						  `sport` varchar(32) character set utf8 collate utf8_polish_ci NOT NULL,
						  PRIMARY KEY  (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
						
						CREATE TABLE IF NOT EXISTS `#__lr_stats_setelements` (
						  `id` int(11) NOT NULL auto_increment,
						  `statset_id` int(11) NOT NULL,
						  `measure_id` int(11) NOT NULL,
						  PRIMARY KEY  (`id`)
						) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;

						INSERT INTO `#__lr_stats_measures` (`id`, `name`, `shortname`, `format`, `description`) VALUES
							(1, 'Points', 'Pts', '%d', ''),
							(2, 'Time Played', 'Min', '%d', ''),
							(3, 'Field Goal Made-Attempted', 'FGM-A', '%d-%d', ''),
							(4, 'Three Points Made-Attempted', '3PM-A', '%d-%d', ''),
							(5, 'Free Throws Made-Attempted', 'FTM-A', '%d-%d', ''),
							(6, 'Rebounds Offensive', 'R-OFF', '%d', ''),
							(7, 'Rebounds Defensive', 'R-DEF', '%d', ''),
							(8, 'Rebounds Total', 'R-TOT', '%d', ''),
							(9, 'Assists', 'AST', '%d', ''),
							(10, 'Fauls', 'PF', '%d', ''),
							(11, 'Steals', 'ST', '%d', ''),
							(12, 'Turnovers', 'TO', '%d', ''),
							(13, 'Blocks Against', 'BA', '%d', ''),
							(14, 'Blocks Shots', 'BS', '%d', '');
							
						INSERT INTO `#__lr_stats_sets` (`id`, `name`, `sport`) VALUES
							(1, 'NBA Boxscore', 'Basketball');
						
						INSERT INTO `#__lr_stats_setelements` (`id`, `statset_id`, `measure_id`) VALUES
							(1, 1, 1),
							(2, 1, 2),
							(3, 1, 3),
							(4, 1, 4),
							(5, 1, 5),
							(6, 1, 6),
							(7, 1, 7),
							(8, 1, 8),
							(9, 1, 9),
							(10, 1, 10),
							(11, 1, 11),
							(12, 1, 12),
							(13, 1, 13),
							(14, 1, 14);
						";
		
		$which_script = JRequest::getVar('which_files');
		
		switch ($which_script){
			case 'all_files':
				$query = $roles_query.$stats_query.$pointslogic_query;
				break;
			case 'roles':
				$query = $roles_query;
				break;
			case 'stats':
				$query = $stats_query;
				break;
			case 'pointslogic':
				$query = $pointslogic_query;
				break;
				
			default:
				break;
		}
		
		 		
		$lr_database = & JFactory::getDBO();

		$lr_database->setQuery ($query);
		if (!$result = $lr_database->queryBatch()) {
	  			$msg .= $lr_database->stderr();
				}
			
		if ($result == 'true') 
    		$msg .= JText::_("RESTORING DICTIONARY QUERIES EXECUTED SUCCESSFULLY!");
		else
			$msg .= JText::_("RESTORING DICTIONARY QUERIES EXECUTION FAILED...");
		
				$mainframe->redirect('index.php?option='.JRequest::getVar('option').'&view=lr_tools', $msg);
		
		parent::display();
	}
}
?>
